package basedeal

import (
	"core/datetime"
	"core/redis"
	"core/server/kafkaserver"
	"core/server/pushserver"
	"errors"
	"iotall/iotcore/entity"
	"iotall/iotdeal/dealconfig"
	"iotall/iotdeal/server"
)

var (
	Deals           map[int64]IDeal
	KafakPushServer *kafkaserver.PushServer
)

func init() {
	Deals = make(map[int64]IDeal, 0)
}
func GetDeal(id int64) (IDeal, error) {
	if v, ok := Deals[id]; ok {
		return v, nil
	}
	return nil, errors.New("处理程序不存在")
}
func AddDeal(deal IDeal) {
	Deals[deal.GetCommPortId()] = deal

}
func StartAll() {
	KafakPushServer = &kafkaserver.PushServer{
		Name:       "数据推送",
		Topic:      entity.EVENTTOPIC,
		PushServer: pushserver.PushServer{},
	}
	KafakPushServer.Start()

	for _, v := range Deals {
		v.Start()
	}
}
func NewStdPkg(Sid string, ReqId, CommPortId, CommProxyId int64) *entity.StdPkg {
	var r = &entity.StdPkg{
		Id:            ReqId,
		Sid:           Sid,
		CrtDt:         datetime.NowStr(),
		CommPortId:    CommPortId,
		CommDealId:    dealconfig.Config.Id,
		CommProxyId:   CommProxyId,
		ClientId:      "",
		PhysicalState: "",
		Events:        make([]entity.StdPkgEvent, 0),
		Devices:       make([]entity.StdPkgDevice, 0),
	}
	return r
}
func PushStdPkg(m *entity.StdPkg) error {

	if len(m.Devices) > 0 {
		var ldvs, err = RedisSaveDevice(m)
		if err != nil {

			return err
		}

		for _, mdev := range *ldvs {
			var mpushdev = &entity.StdPkgDeviceMongo{
				Id:         mdev.Id,
				Sid:        m.Sid,
				Did:        mdev.Did,
				PropValues: mdev.PropValues,
			}
			server.DeviceSvr.Push(*mpushdev)

			err = redis.RedisSetStruct(DevicePrefix+mdev.Id, mdev, 0)
			if err != nil {
				return err
			} else {

			}
		}

		// lold, err := SaveDevice(m)
		// if err != nil {
		// 	return err
		// }

		// for _, mdev := range m.Devices {
		// 	var molddev = GetOldDev(lold, GetDId(m.Sid, mdev.Id))
		// 	for _, vpv := range mdev.Props {
		// 		var moldvalue = GetOldValue(&molddev.PropValues, vpv.Id)
		// 		moldvalue.Sv = vpv.Sv
		// 		moldvalue.Fv = vpv.Fv
		// 		moldvalue.State = vpv.State
		// 		moldvalue.CrtDt = datetime.NowStr()
		// 		if moldvalue.Sv == "" {
		// 			moldvalue.Sv = strconv.FormatFloat(float64(moldvalue.Fv), 'f', -1, 32)
		// 		}
		// 	}

		// 	var mpushdev = &entity.StdPkgDeviceMongo{
		// 		Id:         GetDId(m.Sid, mdev.Id),
		// 		Sid:        m.Sid,
		// 		Did:        mdev.Id,
		// 		PropValues: molddev.PropValues,
		// 	}
		// 	server.DeviceSvr.Push(*mpushdev)
		// }
	}

	var monline = entity.StdPkg{
		Id:            m.Id,
		CommPortId:    m.CommPortId,
		CommDealId:    m.CommDealId,
		CommProxyId:   m.CommProxyId,
		ClientId:      m.ClientId,
		PhysicalState: m.PhysicalState,
		Sid:           m.Sid,
		CrtDt:         m.CrtDt,
		Events:        nil,
		Devices:       nil,
	}
	server.OnlineSvr.Push(monline)

	//在线离线处理
	//模拟量最新值更新

	//var devid = fmt.Sprintf("%v*%v", m.Sid, m.Id)
	//var mold = &entity.StdPkgDeviceMongo{}
	//var err = mongo.FindById("", devid, mold)
	//
	//SaveStdPkgProp(m, mold)
	return nil
	//处理警情
	//处理模拟量
	//处理上下线

	//KafakPushServer.Push("", *m)
	//mylogger.Logger.Info("数据",
	//	zap.String("Dt", datetime.FormatDateTimeMs(time.Now())),
	//	zap.Int64("Id", m.Id),
	//)
}
func SaveStdPkgProp(m *entity.StdPkg, mold *entity.StdPkgDeviceMongo) error {
	return nil
}
func InsertStdPkgProp(m *entity.StdPkg, mold *entity.StdPkgDeviceMongo) error {
	//mold.Id=fmt.Sprintf("%v*%v",m.Sid,m.Id)
	return nil
}
